layout.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import "@/styles/globals.css";
  2. import type { Metadata, Viewport } from "next";
  3. import { notFound } from "next/navigation";
  4. import { Analytics } from "@vercel/analytics/next";
  5. import { ThemeProvider } from "next-themes";
  6. import { Backdrop } from "@/components/backdrop/backdrop";
  7. import { CursorLens } from "@/components/glass/cursor-lens";
  8. import { SiteNav } from "@/components/nav/site-nav";
  9. import { SiteFooter } from "@/components/site-footer";
  10. import { UpdateOnReturn } from "@/components/update-on-return";
  11. import { FontLoader } from "@/components/font-loader";
  12. import { LocaleProvider } from "@/components/locale-provider";
  13. import {
  14. deferredFaces,
  15. fontDisplay,
  16. fontDisplayZh,
  17. fontName,
  18. fontText,
  19. } from "@/config/fonts";
  20. import { siteConfig } from "@/config/site";
  21. import {
  22. alternates,
  23. htmlLang,
  24. isLocale,
  25. localePath,
  26. locales,
  27. ogLocale,
  28. type Locale,
  29. } from "@/lib/i18n";
  30. import { getMessages } from "@/messages";
  31. export function generateStaticParams() {
  32. return locales.map((lang) => ({ lang }));
  33. }
  34. type Props = { params: Promise<{ lang: string }> };
  35. export async function generateMetadata({ params }: Props): Promise<Metadata> {
  36. const { lang } = await params;
  37. if (!isLocale(lang)) return {};
  38. const t = getMessages(lang);
  39. const { meta } = t;
  40. return {
  41. metadataBase: new URL(siteConfig.url),
  42. title: {
  43. default: meta.siteName,
  44. template: `%s - ${meta.siteName}`,
  45. },
  46. description: meta.description,
  47. keywords: siteConfig.keywords,
  48. authors: [{ name: t.hero.name, url: siteConfig.url }],
  49. creator: t.hero.name,
  50. alternates: alternates(lang, "/"),
  51. openGraph: {
  52. type: "website",
  53. locale: ogLocale[lang],
  54. alternateLocale: locales
  55. .filter((l) => l !== lang)
  56. .map((l) => ogLocale[l]),
  57. url: localePath(lang, "/"),
  58. siteName: meta.siteName,
  59. title: meta.siteName,
  60. description: meta.description,
  61. },
  62. twitter: {
  63. card: "summary_large_image",
  64. title: meta.siteName,
  65. description: meta.description,
  66. },
  67. robots: {
  68. index: true,
  69. follow: true,
  70. googleBot: {
  71. index: true,
  72. follow: true,
  73. "max-video-preview": -1,
  74. "max-image-preview": "large",
  75. "max-snippet": -1,
  76. },
  77. },
  78. icons: {
  79. icon: "/favicon.ico",
  80. },
  81. };
  82. }
  83. export const viewport: Viewport = {
  84. themeColor: [
  85. { media: "(prefers-color-scheme: light)", color: "#f6f2f2" },
  86. { media: "(prefers-color-scheme: dark)", color: "#0b0f11" },
  87. ],
  88. };
  89. const jsonLd = (lang: Locale) => ({
  90. "@context": "https://schema.org",
  91. "@graph": [
  92. {
  93. "@type": "WebSite",
  94. "@id": `${siteConfig.url}/#website`,
  95. url: siteConfig.url,
  96. name: getMessages(lang).meta.siteName,
  97. description: getMessages(lang).meta.description,
  98. inLanguage: ["en-US", "zh-CN"],
  99. },
  100. {
  101. "@type": "Person",
  102. "@id": `${siteConfig.url}/#person`,
  103. name: "邓康临",
  104. alternateName: "Deng Kanglin",
  105. url: siteConfig.url,
  106. jobTitle: "Software Engineer",
  107. worksFor: {
  108. "@type": "Organization",
  109. name: "Amazon Web Services",
  110. },
  111. alumniOf: [
  112. {
  113. "@type": "CollegeOrUniversity",
  114. name: "University of California, Los Angeles",
  115. },
  116. {
  117. "@type": "CollegeOrUniversity",
  118. name: "Purdue University",
  119. },
  120. ],
  121. sameAs: [siteConfig.links.github, siteConfig.links.linkedin],
  122. },
  123. ],
  124. });
  125. export default async function RootLayout({
  126. children,
  127. params,
  128. }: Props & { children: React.ReactNode }) {
  129. const { lang } = await params;
  130. if (!isLocale(lang)) notFound();
  131. const t = getMessages(lang);
  132. return (
  133. <html
  134. suppressHydrationWarning
  135. className={`${fontText.variable} ${fontDisplay.variable} ${fontName.variable} ${fontDisplayZh.variable}`}
  136. lang={htmlLang[lang]}
  137. >
  138. <head>
  139. {/* Returning visitors have the display face cached: use it from the first frame. */}
  140. <script
  141. dangerouslySetInnerHTML={{
  142. __html:
  143. 'try{localStorage.getItem("ff")&&document.documentElement.classList.add("ff")}catch(e){}',
  144. }}
  145. />
  146. <script
  147. dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd(lang)) }}
  148. type="application/ld+json"
  149. />
  150. </head>
  151. <body>
  152. <ThemeProvider
  153. disableTransitionOnChange
  154. enableSystem
  155. attribute="class"
  156. defaultTheme="system"
  157. >
  158. <LocaleProvider lang={lang}>
  159. <a
  160. className="sr-only fixed top-3 left-3 z-[80] rounded-full bg-ink px-4 py-2 text-bg focus:not-sr-only"
  161. href="#main"
  162. >
  163. {t.meta.skip}
  164. </a>
  165. <Backdrop />
  166. <SiteNav />
  167. <main
  168. className="relative z-10 mx-auto w-full max-w-[1240px] grow px-3 pt-6 pb-6 sm:px-6 sm:pt-24 sm:pb-9"
  169. id="main"
  170. >
  171. {children}
  172. </main>
  173. <SiteFooter lang={lang} />
  174. <CursorLens />
  175. <UpdateOnReturn />
  176. </LocaleProvider>
  177. </ThemeProvider>
  178. <FontLoader faces={deferredFaces} />
  179. <Analytics />
  180. </body>
  181. </html>
  182. );
  183. }